<template>
  <div class="forget-pwd">
    <div class="forget-block">
      <div class="search-user">
        <!-- 查询用户是否存在 -->
        <el-form
          ref="searchUserForm"
          :model="searchUserForm"
          :rules="searchUserRules"
          label-position="top"
        >
          <div class="search-username">
            <el-form-item label="客户姓名" prop="username">
              <el-input
                type="text"
                maxlength="10"
                placeholder="请输入客户姓名"
                v-model="searchUserForm.username"
              />
            </el-form-item>
          </div>

          <div class="search-usercard">
            <el-form-item label="身份证号码" prop="userCard">
              <el-input
                type="text"
                maxlength="18"
                placeholder="请输入身份证号码"
                v-model="searchUserForm.userCard"
              />
            </el-form-item>
          </div>

          <div class="search-button">
            <el-button
              type="primary"
              :loading="searchLoading"
              v-on:click="searchUser('searchUserForm')"
            >
              <span v-if="!searchLoading">查询客户</span>
              <span v-else>查 询 中...</span>
            </el-button>
          </div>

          <div class="toLogin-button">
            <router-link
              to="/login"
              style="text-decoration: none; color: black"
            >
              <el-button>返回</el-button>
            </router-link>
          </div>
        </el-form>
      </div>

      <div class="modify-password-form">
        <el-form
          ref="modifyPasswordForm"
          :model="modifyPasswordForm"
          :rules="modifyPasswordRules"
          label-position="top"
        >
          <div class="modify-password">
            <el-form-item label="密码" prop="password">
              <el-input
                type="text"
                maxlength="10"
                minlength="6"
                placeholder="请输入新密码(6-10位)"
                v-model="modifyPasswordForm.password"
                show-password
              />
            </el-form-item>
          </div>

          <div class="modify-confirm-password">
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input
                type="text"
                maxlength="10"
                minlength="6"
                placeholder="请确认密码"
                v-model="modifyPasswordForm.confirmPassword"
                show-password
              />
            </el-form-item>
          </div>

          <div class="modify-button">
            <el-button
              type="primary"
              :loading="modifyLoading"
              v-on:click="modifyPwd('modifyPasswordForm')"
            >
              <span v-if="!modifyLoading">修改密码</span>
              <span v-else>修 改 中...</span>
            </el-button>
          </div>

          <div class="toLogin-button">
            <router-link
              to="/login"
              style="text-decoration: none; color: black"
            >
              <el-button>返回</el-button>
            </router-link>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import { searchUserInfo, modifyPassword } from "@/api/user";
export default {
  name: "ForgetPwd",

  data() {
    var checkConfirmPassword = (rule, value, callback) => {
      if (value === "") callback(new Error("请确认密码"));
      else if (value != this.modifyPasswordForm.password)
        callback(new Error("两次输入密码不一致"));
      else callback();
    };
    return {
      searchLoading: false,
      modifyLoading: false,
      searchUserForm: {
        username: "",
        userCard: "",
      },

      searchUserRules: {
        username: [
          {
            required: true,
            message: "请输入客户姓名",
            trigger: "blur",
          },
        ],
        userCard: [
          {
            required: true,
            message: "请输入身份证号码",
            trigger: "blur",
          },
        ],
      },

      modifyPasswordForm: {
        password: "",
        confirmPassword: "",
      },

      modifyPasswordRules: {
        password: [
          {
            required: true,
            message: "请输入新密码",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          {
            required: true,
            validator: checkConfirmPassword,
            trigger: "blur",
          },
        ],
      },
    };
  },

  methods: {
    searchUser(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.searchLoading = true;
          searchUserInfo(
            this.searchUserForm.username,
            this.searchUserForm.userCard
          )
            .then((res) => {
              document.getElementsByClassName("search-user")[0].style.display =
                "none";
              document.getElementsByClassName(
                "modify-password-form"
              )[0].style.display = "block";
            })
            .catch((err) => {
              this.searchLoading = false;
              return false;
            });
        } else {
          this.searchLoading = false;
          return false;
        }
      });
    },

    modifyPwd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.modifyLoading = true;
          modifyPassword(
            this.searchUserForm.userCard,
            this.modifyPasswordForm.password
          )
            .then((res) => {
              this.$message.success("修改成功,请登录");
              this.$router.push("/login");
            })
            .catch((err) => {
              this.modifyLoading = false;
              return false;
            });
        } else {
          this.modifyLoading = false;
          return false;
        }
      });
    },
  },
};
</script>

<style>
.forget-pwd {
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: url("../assets/login.png");
  width: 100%;
  height: 100%;
  position: fixed;
  background-size: 100% 100%;
}

.forget-block {
  margin-top: 15%;
  margin-right: 10%;
  float: right;
  width: 350px;
  height: 300px;
  border: 1px solid #ddd;
  background: #fff;
}

.modify-password-form {
  display: none;
}

.search-user,
.modify-password-form {
  height: 300px;
  width: 300px;
  padding-top: 15px;
}

.search-user .el-form-item,
.modify-password-form .el-form-item {
  margin-bottom: 10px;
  padding-left: 20px;
  padding-top: 10px;
}

.search-user .el-form-item label,
.modify-password-form .el-form-item label {
  padding-bottom: 0;
}

.search-button,
.toLogin-button,
.modify-button {
  display: inline-block;
  width: 100px;
  margin-left: 30px;
  margin-top: 20px;
}

.search-button .el-button,
.modify-button .el-button {
  width: 130px;
}

.toLogin-button .el-button {
  width: 130px;
  margin-left: 20px;
}
</style>